package cn.com.xxlibrary.action;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.stereotype.Controller;

import cn.com.xxlibrary.orm.ManagerInfoBean;
import cn.com.xxlibrary.service.IManagerService;
import cn.com.xxlibrary.service.ISystemService;
import cn.com.xxlibrary.util.UtilTools;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 管理员操作Action
 * 
 * @author 学刚
 * 
 */
@Controller
@ParentPackage("myStruts")
@Namespace(value = "/")
// @ExceptionMappings(value = { @ExceptionMapping(exception =
// "java.lang.Exception", result = "error", params={"exception"}) })
@Results(value = { @Result(name = "login", location = "/login.jsp"),
		@Result(name = "back", location = "goBack.jsp") })
public class ManagerAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	ManagerInfoBean manager;
	int id, state;

	String oldPwd, newPwd;

	String option;

	// 默认页码
	private int pageNo = 1;
	// 默认分页大小
	private int pageSize = 10;

	@Resource
	IManagerService manService;


	@Resource
	private ISystemService systemService;

	/**
	 * 管理员登录
	 * 
	 * @return
	 */
	@Action(value = "login", results = {
			@Result(name = SUCCESS, location = "/mainFrame", type="redirect"),
			@Result(name = "Guest", location = "/search", type="redirect") })
	public String login() throws Exception {
		manager = manService.login(manager);
		String re = "";
		if (UtilTools.isNotNullorEmpty(manager)) {
			ActionContext.getContext().getSession()
					.put("SESSION_MANAGER_LOGIN", manager);
			if ("游客".equals(manager.getManType().getManTypeName())) {
				re = "Guest";
			} else {
				re = SUCCESS;
			}

			// 登陆成功，记录日志
			//获取IP
			String IP = ServletActionContext.getRequest()
					.getRemoteAddr();
			String strLog = "登录成功";
			systemService.recordLog(strLog, manager, IP);

		} else {
			re = LOGIN;
		}

		return re;
	}

	/**
	 * 注销登录
	 * 
	 * @return
	 */
	@Action(value = "logout", results = { @Result(name = SUCCESS, location = "/login.jsp") })
	public String logout() {
		if (UtilTools.isNotNullorEmpty(ActionContext.getContext().getSession()
				.get("SESSION_MANAGER_LOGIN"))) {
			manager = (ManagerInfoBean) ActionContext.getContext().getSession()
					.get("SESSION_MANAGER_LOGIN");
			
			// 记录日志
			//获取IP
			String IP = ServletActionContext.getRequest()
					.getRemoteAddr();
			String strLog = "注销";
			systemService.recordLog(strLog, manager, IP);
		}

		ActionContext.getContext().getSession()
				.remove("SESSION_MANAGER_LOGIN");
		ActionContext.getContext().getSession().clear();

		return SUCCESS;
	}

	/**
	 * 添加管理员
	 * 
	 * @return
	 */
	@Action(value = "addManager", results = {
			@Result(name = SUCCESS, location = "/listManager", type = "redirect"),
			@Result(name = "enter", location = "/manager/managerDetail.jsp") })
	public String addManager() {
		List types = manService.listManagerType();
		ActionContext.getContext().getSession()
				.put("SESSION_MANAGER_TYPE", types);
		if ("enter".equals(option)) {
			return "enter";
		} else {
			manService.addManager(manager);



			return SUCCESS;
		}
	}

	/**
	 * 删除管理员（不可恢复）
	 * 
	 * @return
	 */
	@Action(value = "delManager", results = { @Result(name = SUCCESS, location = "/listManager", type = "redirect") })
	public String delManager() {
		
		// 记录日志
		//获取IP
		String IP = ServletActionContext.getRequest()
				.getRemoteAddr();
		String strLog = "删除管理员: "+manService.queManager(id).getManLoginName();
		ManagerInfoBean man = (ManagerInfoBean) ActionContext.getContext()
				.getSession().get("SESSION_MANAGER_LOGIN");
		systemService.recordLog(strLog, man, IP);

		manService.delManager(id);
		
		return SUCCESS;
	}

	/**
	 * 查询所有管理员信息
	 * 
	 * @return
	 */
	@Action(value = "listManager", results = { @Result(name = SUCCESS, location = "/manager/managerList.jsp") })
	public String listManager() {

		int start = (pageNo - 1) * pageSize;

		Map map = manService.listManager(start, pageSize);

		List<?> list = (List<?>) map.get("queryResult");
		int count = (Integer) map.get("count");
		ActionContext.getContext().getSession()
				.put("SESSION_MANAGER_LIST", list);
		ActionContext.getContext().getSession()
				.put("SESSION_MANAGER_PAGESIZE", pageSize);
		ActionContext.getContext().getSession()
				.put("SESSION_MANAGER_PAGENO", pageNo);
		ActionContext.getContext().getSession()
				.put("SESSION_MANAGER_COUNT", count);
		
		// 记录日志
		//获取IP
		String IP = ServletActionContext.getRequest()
				.getRemoteAddr();
		String strLog = "查询所有的管理员信息";
		ManagerInfoBean manager = (ManagerInfoBean) ActionContext.getContext()
				.getSession().get("SESSION_MANAGER_LOGIN");
		systemService.recordLog(strLog, manager, IP);

		return SUCCESS;
	}

	/**
	 * 查询指定ID的管理员
	 * 
	 * @return
	 */
	@Action(value = "queManager", results = { @Result(name = SUCCESS, location = "/manager/managerDetail.jsp") })
	public String queManager() {
		List types = manService.listManagerType();
		ActionContext.getContext().getSession()
				.put("SESSION_MANAGER_TYPE", types);
		manager = manService.queManager(id);
		ActionContext.getContext().getSession()
				.put("SESSION_MANAGER_QUERY_RESULT", manager);
		
		// 记录日志
		//获取IP
		String IP = ServletActionContext.getRequest()
				.getRemoteAddr();
		String strLog = "查找管理员: "+manager.getManLoginName();
		ManagerInfoBean man = (ManagerInfoBean) ActionContext.getContext()
				.getSession().get("SESSION_MANAGER_LOGIN");
		systemService.recordLog(strLog, man, IP);

		return SUCCESS;
	}

	/**
	 * 修改密码
	 * 
	 * @return
	 */
	@Action(value = "changePwd", results = { @Result(name = SUCCESS, location = "/manager/changePwd.jsp") })
	public String changePwd() {
		if ("enter".equals(option)) {
			ActionContext.getContext().getSession().put("SESSION_MSG", null);
			return SUCCESS;
		} else {
			manager = (ManagerInfoBean) ActionContext.getContext().getSession()
					.get("SESSION_MANAGER_LOGIN");
			if (oldPwd.equals(manager.getManPassword())) {
				manService.changePwd(manager, newPwd);
				ActionContext.getContext().getSession()
						.put("SESSION_MANAGER_LOGIN", null);
				ActionContext.getContext().getSession()
						.put("SESSION_MSG", "密码修改成功,请使用新密码重新登录!");
				
				// 记录日志
				//获取IP
				String IP = ServletActionContext.getRequest()
						.getRemoteAddr();
				String strLog = "修改密码";
				systemService.recordLog(strLog, manager, IP);


			} else {
				ActionContext.getContext().getSession()
						.put("SESSION_MSG", "密码修改失败,请重试!");
			}
		}
		return SUCCESS;
	}

	/**
	 * 重置密码
	 * @return
	 * @throws IOException
	 */
	@Action(value = "resetPwd")
	public void resetPwd() throws IOException {
		// d6a9a933c8aafc51e55ac0662b6e4d4a
		manService.resetPwd(id, "d6a9a933c8aafc51e55ac0662b6e4d4a");
		
		// 记录日志
		//获取IP
		String IP = ServletActionContext.getRequest()
				.getRemoteAddr();
		String strLog = "重置 "+manService.queManager(id).getManLoginName()+" 密码";
		ManagerInfoBean manager = (ManagerInfoBean) ActionContext.getContext()
				.getSession().get("SESSION_MANAGER_LOGIN");
		systemService.recordLog(strLog, manager, IP);


		UtilTools.sendMsg("重置成功, 请尽快登录修改! ");
	}

	// ========================Getters and Setters========================

	public ManagerInfoBean getManager() {
		return manager;
	}

	public void setManager(ManagerInfoBean manager) {
		this.manager = manager;
	}

	public IManagerService getManService() {
		return manService;
	}

	public void setManService(IManagerService manService) {
		this.manService = manService;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getState() {
		return state;
	}

	public void setState(int state) {
		this.state = state;
	}

	public String getOldPwd() {
		return oldPwd;
	}

	public void setOldPwd(String oldPwd) {
		this.oldPwd = oldPwd;
	}

	public String getNewPwd() {
		return newPwd;
	}

	public void setNewPwd(String newPwd) {
		this.newPwd = newPwd;
	}

	public String getOption() {
		return option;
	}

	public void setOption(String option) {
		this.option = option;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public ISystemService getSystemService() {
		return systemService;
	}

	public void setSystemService(ISystemService systemService) {
		this.systemService = systemService;
	}

}
